﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_PageList : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string text2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            this.PageLink.NavigateUrl = "Page.aspx?menuId=" + str;
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string whereStr = string.Format("MenuId={0}", str);
            DataTable oldTable = db.DataTable("OrderId,Title,FileName,ParentId,SinglePageId,PageDescribe", "SinglePage", whereStr, "OrderId ASC");
            db.Close();
            oldTable.Columns.Add("TitleText");
            DataTable newTable = oldTable.Clone();
            Function.PopulateTable(oldTable, newTable, "Title", "SinglePageId", "ParentId", 0, 0);
            this.PageList.DataSource = newTable;
            this.PageList.DataBind();
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string str = this.PageList.DataKeys[this.PageList.EditIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count(FileName)", "SinglePage", string.Format("FileName='{0}' AND SinglePageId<>{1}", args.Value.ToString(), str));
        db.Close();
        if (num > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }

    protected void PageList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.PageList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void PageList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.PageList.EditIndex = -1;
        this.Bind();
    }

    protected void PageList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        HiddenField field = (HiddenField)e.Row.FindControl("ParentIdHiddenField");
        string str = "";
        if (field != null)
        {
            str = field.Value.ToString();
        }
        if (str == "0")
        {
            ((Button)e.Row.FindControl("DelBt")).Visible = false;
            ((Button)e.Row.FindControl("EditBt")).Visible = false;
        }
        int num = e.Row.DataItemIndex - (this.PageList.PageIndex * this.PageList.PageSize);
        if (num >= 0)
        {
            string str2 = this.PageList.DataKeys[num].Values[0].ToString();
            Button button = (Button)e.Row.FindControl("EditBt");
            Button control = (Button)e.Row.FindControl("DelBt");
            HyperLink link = (HyperLink)e.Row.FindControl("EditHyperLink");
            if (control != null)
            {
                if (!ManagerLogin.ValidateRole(string.Format("0_{0}_a", str2)))
                {
                    button.Enabled = false;
                }
                if (!ManagerLogin.ValidateRole(string.Format("0_{0}_b", str2)))
                {
                    control.Enabled = false;
                }
                else
                {
                    ClientScriptManager clientScript = this.Page.ClientScript;
                    string str3 = "";
                    str3 = clientScript.GetPostBackEventReference(control, "").Replace("'", @"\'");
                    control.OnClientClick = "return myConfirm('确认操作','将同时删除此栏目及其所有子栏目<br />确定删除吗?','" + str3 + "');";
                }
                if (!ManagerLogin.ValidateRole(string.Format("0_{0}_c", str2)))
                {
                    link.Enabled = false;
                }
            }
        }
    }

    protected void PageList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string parentId = this.PageList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        string str2 = Function.ChildIdList(db.DataTable("SinglePageId,ParentId", "SinglePage", string.Format("menuId={0}", (string)this.Session["MenuId"]), ""), "SinglePageId", "ParentId", parentId, true);
        db.Delete("SinglePage", string.Format("SinglePageId IN({0})", str2));
        db.Close();
        this.Bind();
        this.Msg.Text = "栏目删除成功！";
        this.Msg.Visible = true;
    }

    protected void PageList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.PageList.EditIndex = e.NewEditIndex;
        this.Bind();
    }

    protected void PageList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (base.IsValid)
        {
            int rowIndex = e.RowIndex;
            string str = ((TextBox)this.PageList.Rows[rowIndex].Cells[0].Controls[0]).Text.ToString();
            string str2 = ((TextBox)this.PageList.Rows[rowIndex].FindControl("PageTitle")).Text.ToString();
            string str3 = ((TextBox)this.PageList.Rows[rowIndex].FindControl("FileName")).Text.ToString();
            string str5 = ((TextBox)this.PageList.Rows[rowIndex].FindControl("desc")).Text.ToString();
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string[,] includeColumns = new string[,] { { "Title", str2, "NVarChar", "0" }, { "FileName", str3, "NVarChar", "32" }, { "OrderId", str, "int", "0" },{"PageDescribe",str5,"NVarChar","100"} };
            string str4 = this.PageList.DataKeys[rowIndex].Values[0].ToString();
            string whereStr = string.Format("SinglePageId={0}", str4);
            db.Update("SinglePage", includeColumns, whereStr);
            db.Close();
            this.Msg.Text = "子栏目编辑成功！";
            this.Msg.Visible = true;
        }
        this.PageList.EditIndex = -1;
        this.Bind();
    }
}
